<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\Common;
class Upload
{
    protected $request;
    protected $config;
    protected $uniacid;
    protected $path_type;
    protected $attachment_model;
    protected $is_weiqin = false;

    public function __construct($uniacid = "7777")
    {
        $this->uniacid = $uniacid;
        $this->config = longbingGetOssConfig($uniacid);
        $this->is_weiqin = longbingIsWeiqin();
    }

    public function upload($type, $file, $config = [], $is_check = "")
    {
        if (!empty($config)) {
            $this->config = $config;
        }
        $base_path = "/";
        $type_data = 1;
        switch ($type) {
            case "picture":
                $base_path = $base_path . "image/";
                $type_data = 1;
                break;
            case "audio":
                $base_path = $base_path . "audio/";
                $type_data = 2;
                break;
            case "video":
                $base_path = $base_path . "video/";
                $type_data = 3;
                break;
            case "cert":
                $base_path = $base_path . "cert/";
                $type_data = 4;
                break;
            case "wxuploadkey":
                $base_path = $base_path . "wxuploadkey/";
                $type_data = 5;
                break;
            case "file":
                $base_path = $base_path . "file/";
                $type_data = 6;
                break;
            case "local_picture":
                $base_path = $base_path . "local_picture/";
                $type_data = 7;
                break;
            default:
                $base_path = $base_path . "image/";
                $type_data = 1;
        }
        $base_path = $base_path . $this->uniacid . "/" . date("y") . "/" . date("m");
        $info = NULL;
        $upload_status = false;
        if ($this->checkFile($type, $file, $is_check)) {

            $file_name = NULL;
            if (in_array($type, ["cert", "wxuploadkey", "local_picture"])) {
                $file_name = $this->uniacid . "_" . $file->getOriginalName();
                $this->config["open_oss"] = 0;
            }
            $file_name .= $is_check;
            $info_path = $this->fileLoaclSave($base_path, $file, $file_name);
            $info = $this->fileInfo($info_path, $file->getOriginalName() . $is_check, $type_data);

            if (isset($this->config["open_oss"])) {
                switch ($this->config["open_oss"]) {
                    case 0:
                        $upload_status = true;
                        $info["longbing_driver"] = "loacl";
                        break;
                    case 1:
                        $oss_res = $this->aliyunUpload($info_path);
                        if (isset($this->config["aliyun_base_dir"]) && !empty($this->config["aliyun_base_dir"])) {
                            $info_path = $this->config["aliyun_base_dir"] . "/" . $info_path;
                        }
                        if (in_array(substr($info_path, 0, 1), ["/", "/"])) {
                            $info_path = substr($info_path, 1, strlen($info_path) - 1);
                        }
                        $info["attachment"] = $info_path;
                        $info["longbing_driver"] = "aliyun";
                        if (isset($oss_res["info"]["url"])) {
                            $upload_status = true;
                        }
                        break;
                    case 2:
                        $oss_res = $this->qiniuUpload($info_path);
                        $info["longbing_driver"] = "qiniuyun";
                        if (!empty($oss_res) && empty($oss_res[1])) {
                            $upload_status = true;
                        }
                        break;
                    case 3:
                        $oss_res = $this->tenxunUpoload($info_path);
                        $info["longbing_driver"] = "tengxunyun";
                        if (isset($oss_res["ETag"]) && isset($oss_res["ObjectURL"])) {
                            $upload_status = true;
                        }
                        break;
                    default:
                        $info["longbing_driver"] = "loacl";
                        $upload_status = true;
                }
            } else {
                $upload_status = true;
                $info["longbing_driver"] = "loacl";
            }
        }
        if (!$upload_status) {
            $info = NULL;
        }
        return $info;
    }

    public function checkFile($type, $file, $is_check = "")
    {
        $result = false;
        switch ($type) {
            case "picture":
                $result = validate(["file" => ["fileSize" => 8388608, "fileExt" => "jpg,jpeg,bmp,png,image,gif"]])->check(["file" => $file]);
                break;
            case "audio":
                $check = empty($is_check) ? "mp3,wma,wav,m4a" : "";
                $result = validate(["file" => ["fileSize" => 209715200, "fileExt" => $check]])->check(["file" => $file]);
                break;
            case "video":
                $result = validate(["file" => ["fileSize" => 52428800, "fileExt" => "wmv,mp4,avi,mpg,rmvb,MPEG-4,MPEG,MOV,3GP,MPV,quicktime,Quicktime,mov"]])->check(["file" => $file]);
                break;
            case "cert":
                $result = validate(["file" => ["fileSize" => 52428800, "fileExt" => "cert,pem,zip"]])->check(["file" => $file]);
                break;
            case "wxuploadkey":
                $result = validate(["file" => ["fileSize" => 52428800, "fileExt" => "cert,pem,key"]])->check(["file" => $file]);
                break;
            case "file":
                $result = validate(["file" => ["fileSize" => 52428800, "fileExt" => "doc,xls,docx,xlsx,ppt,pptx,pdf,zip"]])->check(["file" => $file]);
                break;
            case "local_picture":
                $result = validate(["file" => ["fileSize" => 8388608, "fileExt" => "jpg,jpeg,bmp,png,image,gif"]])->check(["file" => $file]);
                break;

        }
        return $result;
    }

    public function fileLoaclSave($path, $file, $file_name = NULL)
    {
        if (empty($file_name)) {
            $file_name = uuid() . "." . $file->getOriginalExtension();
        }
        $info = \think\facade\Filesystem::disk("longbing")->putFileAs($path, $file, $file_name);
        return $info;
    }

    public function fileInfo($path, $file_name, $type_data)
    {
        $result = ["attachment" => ltrim($path, "/"), "uniacid" => $this->config["uniacid"], "filename" => $file_name, "createtime" => time(), "type" => $type_data];
        return $result;
    }

    public function aliyunUpload($path)
    {
        $path = ltrim($path, "/");
        try {
            $bucket = $this->config["aliyun_bucket"];
            $filePath = FILE_UPLOAD_PATH . $path;
            if (isset($this->config["aliyun_base_dir"]) && !empty($this->config["aliyun_base_dir"])) {
                $path = $this->config["aliyun_base_dir"] . "/" . $path;
            }
            if (in_array(substr($path, 0, 1), ["/", "/"])) {
                $path = substr($path, 1, strlen($path) - 1);
            }
            if (file_exists($filePath)) {
                require_once dirname(__FILE__) . "/extend/aliyuncs/oss-sdk-php/autoload.php";
                $ossClient = new \OSS\OssClient($this->config["aliyun_access_key_id"], $this->config["aliyun_access_key_secret"], $this->config["aliyun_endpoint"]);
                $res = $ossClient->uploadFile($bucket, $path, $filePath);
                if (is_file($filePath)) {
                    unlink($filePath);
                }
                return $res;
            }
        } catch (\OSS\Core\OssException $e) {
            return false;
        }
        return false;
    }

    public function uploadFile($info_path, $type = 1)
    {
        $info = [];
        $upload_status = false;
        if (isset($this->config["open_oss"])) {
            switch ($this->config["open_oss"]) {
                case 0:
                    $upload_status = true;
                    $info["longbing_driver"] = "loacl";
                    break;
                case 1:
                    $oss_res = $this->aliyunUpload($info_path);
                    if (isset($this->config["aliyun_base_dir"]) && !empty($this->config["aliyun_base_dir"])) {
                        $info_path = $this->config["aliyun_base_dir"] . "/" . $info_path;
                    }
                    if (in_array(substr($info_path, 0, 1), ["/", "/"])) {
                        $info_path = substr($info_path, 1, strlen($info_path) - 1);
                    }
                    $info["attachment"] = $info_path;
                    $info["longbing_driver"] = "aliyun";
                    if (isset($oss_res["info"]["url"])) {
                        $upload_status = true;
                    }
                    break;
                case 2:
                    $oss_res = $this->qiniuUpload($info_path);
                    $info["longbing_driver"] = "qiniuyun";
                    if (empty($oss_res[1])) {
                        $upload_status = true;
                    }
                    break;
                case 3:
                    $oss_res = $this->tenxunUpoload($info_path);
                    $info["longbing_driver"] = "tengxunyun";
                    if (isset($oss_res["ETag"]) && isset($oss_res["ObjectURL"])) {
                        $upload_status = true;
                    }
                    break;
                default:
                    $info["longbing_driver"] = "loacl";
                    $upload_status = true;
            }
        } else {
            $upload_status = true;
            $info["longbing_driver"] = "loacl";
        }
        if ($type == 1) {
            return $upload_status;
        }
        $info["status"] = $upload_status;
        return $info;
    }

    public function qiniuUpload($path)
    {
        try {
            require_once dirname(__FILE__) . "/extend/qiniu/autoload.php";
            $path = ltrim($path, "/");
            $accessKey = $this->config["qiniu_accesskey"];
            $secretKey = $this->config["qiniu_secretkey"];
            $bucket = $this->config["qiniu_bucket"];
            $auth = new \Qiniu\Auth($accessKey, $secretKey);
            $token = $auth->uploadToken($bucket);
            $filePath = FILE_UPLOAD_PATH . $path;
            $client = new \Qiniu\Storage\UploadManager();
            list($ret, $err) = $client->putFile($token, $path, $filePath);
            if (is_file($filePath)) {
                unlink($filePath);
            }
            return [$ret, $err];
        } catch (\Exception $e) {
            return false;
        }
    }

    public function tenxunUpoload($path)
    {
        try {
            $path = ltrim($path, "/");
            require dirname(__FILE__) . "/extend/tencentcloud/vendor/autoload.php";
            $appid = $this->config["tenxunyun_appid"];
            $secretid = $this->config["tenxunyun_secretid"];
            $secretkey = $this->config["tenxunyun_secretkey"];
            $bucket = $this->config["tenxunyun_bucket"];
            $region = $this->config["tenxunyun_region"];
            $yuming = $this->config["tenxunyun_yuming"];
            $cosClient = new \Qcloud\Cos\Client(["region" => $region, "credentials" => ["secretId" => $secretid, "secretKey" => $secretkey]]);
            $body = FILE_UPLOAD_PATH . $path;
            try {
                $result = $cosClient->putObject(["Bucket" => $bucket, "Key" => $path, "Body" => file_get_contents($body)]);
                if (is_file($body)) {
                    unlink($body);
                }
                return $result;
            } catch (\Exception $e) {
                return false;
            }
        } catch (\Exception $e) {
            return false;
        }
    }

    public function checkFileV2($type, $file)
    {
        $result = false;
        switch ($type) {
            case "picture":
                $result = validate(["image" => "filesize:2097152|fileExt:jpg,jpeg,bmp,png|image:*"])->check([$file]);
                break;
            case "audio":
                $result = validate(["audio" => "filesize:2097152|fileExt:mp3,wma,wav,m4a"])->check([$file]);
                break;
            case "video":
                $result = validate(["video" => "filesize:2097152|fileExt:wmv,mp4,avi,mpg,rmvb"])->check([$file]);
                break;
            case "cert":
                $result = validate(["cert" => "filesize:2097152|fileExt:cert"])->check([$file]);
                break;
            case "wxuploadkey":
                $result = validate(["cert" => "filesize:2097152|fileExt:cert"])->check([$file]);
                break;
            default:

        }
        return $result;
    }
}

?>